package cn.learn.server;

import cn.learn.proto.CreateResponse;
import cn.learn.server.DataTree.ProcessTxnResult;
import cn.learn.server.util.RequestPathMetricsCollector;

public class FinalRequestProcessor implements RequestProcessor{
    ZooKeeperServer zks;
    private final RequestPathMetricsCollector requestPathMetricsCollector;

    public FinalRequestProcessor(ZooKeeperServer zks) {
        this.zks = zks;
        this.requestPathMetricsCollector = zks.getRequestPathMetricsCollector();
    }

    @Override
    public void processRequest(Request request) throws RequestProcessorException {
        System.out.println("final处理器开始处理。。。");
        ProcessTxnResult rc = null;
        rc = applyRequest(request);
        Record rsp = null;
        int responseSize = 0;
        NettyServerCnxn cnxn = request.cnxn;

        String path = null;
        try {
            switch (request.type) {
                case 1:
//                    lastOp = "CREA";
                    rsp = new CreateResponse(rc.path);
//                    requestPathMetricsCollector.registerRequest(request.type, rc.path);
                    break;
            }
            if (path == null) {
                responseSize = cnxn.sendResponse(rsp, "response");
            }else {
                int opCode = 1;
//                responseSize = cnxn.sendResponse(hdr, rsp, "response");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        // 返回结果
//        responseSize = cnxn.sendResponse(hdr, rsp, "response");
    }

    private ProcessTxnResult applyRequest(Request request) {
        ProcessTxnResult rc = zks.processTxn(request);
        return rc;
    }

    @Override
    public void shutdown() {

    }
}
